import router from '@/router'
import store from '@/store'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']
// 前守卫
// to 目标路由   from 当前路由   next 要跳转的路由
router.beforeEach((to, from, next) => {
  nProgress.start()
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
      nProgress.done()
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
      nProgress.done()
    }
  }
})

// 后守卫 路由跳转完毕执行
router.afterEach(() => nProgress.done())
